Multidimensional Array and 2-D Array in C++
Multidimensional array is an array that has more than one dimension, allowing you to represent data in a two-dimensional, three-dimensional, or even higher-dimensional structure. The most common multidimensional arrays are two-dimensional arrays, but C allows you to create arrays with more dimensions as well.
Types of Multidimensional array in C
1. One dimensional Array(1D Array)
2. Two dimensional Array(2D Array)
3. Three dimensional Array(3D Array)
In our previous article, we have understood about One Dimension Array(1-D) or we have read its syntax, declaration, Iteration or accessing the elements or in this tutorial we will learn about Two Dimensional Array(2D)
Two Dimensional Array
A two-dimensional array in C is a type of array that has two dimensions, often used to represent a grid, matrix, or table-like data structure. It is essentially an array of arrays, where each element of the array is itself an array.
A two-dimensional array is essentially an array of arrays. It represents a table or grid structure with rows and columns.
Array Declaration
Syntax of 2-D Array Declaration:
Example 2-D of Array Declaration:
int array[4][4];
This creates an array with 3 rows and 4 columns of integers. The array elements are uninitialized and could contain garbage values.
Initialization of 2-D Arrays
There are a couple of ways to start up a 2D array:
1. Using Initializer List: You can set up a 2D array by listing all the elements you want inside braces { }.
2. Using Loops: Another way is to employ loops, like for loops, to go through each element and assign values as needed.
Two Dimensional Array Declaration using Initializer list:
You can also initialize a two-dimensional array using an initializer list:
Two Dimensional Array Declaration using Loops:
You can use nested loops to initialize a two-dimensional array:
int cols = 4;
int array[3][4];
int value = 1;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
array[i][j] = value;
value++;
}
}
Accessing Elements of Two Dimensional Array :
To access elements in a two-dimensional array in C++, you use the row and column indices. You can access individual elements of an array using the square bracket notation:
Example for Accessing an 2-D Array :
Note:
1. In C++, multidimensional arrays are stored in row-major order, meaning the elements of each row are contiguous in memory.
2. Ensure bounds checking to avoid accessing elements outside the defined range to prevent unexpected behavior or crashes.
Two-dimensional arrays are incredibly useful for representing grids, tables, matrices, and more complex data structures in programming.Example of 2-D Array
// Main function
int main() {
// Declaration and initialization of a 2D integer array
int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
// Displaying the 2D array
std::cout << "2D Array elements:\n";
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 4; ++j) {
std::cout << matrix[i][j] << " ";
}
std::cout << '\n';
}
return 0;
}